from flask import Blueprint, render_template, request, jsonify
from flask_login import login_required

from applications.common.curd import model_to_dicts, enable_status, disable_status, get_one_by_id
from applications.common.utils.http import table_api, success_api, fail_api
from applications.common.utils.rights import authorize
from applications.common.utils.validate import str_escape
from applications.extensions import db
from applications.models import Weixin_Official_Acc_Group
from applications.schemas import RoleOutSchema, PowerOutSchema2
admin_group = Blueprint('adminGroup', __name__, url_prefix='/admin/group')
from applications.common.appenv import HttpBaseCode

# 表格数据
@admin_group.get('/data')
@login_required
def groupdata():
    result = {}
    try:
        res = db.session.query(Weixin_Official_Acc_Group).order_by(Weixin_Official_Acc_Group.code.asc()).all()
        data = []
        for item in res:
            group = {}
            group['id'] = item.id
            group['name'] = item.name
            group['code'] = item.code
            data.append(group)
        result['data'] = data
        result['count'] = len(res)
        result['code'] = HttpBaseCode.SUCCESS_CODE
        result['message'] = HttpBaseCode.SUCCESS_MESSAGE
    except Exception as e:
        result['code'] = HttpBaseCode.FAIL_CODE
        result['message'] = HttpBaseCode.FAIL_MESSAGE
        print(e)
    return jsonify(result)




